package io.xiyou.order.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.xiyou.order.api.IOrderService;
import io.xiyou.order.api.bo.AddOrderBO;
import io.xiyou.order.api.bo.PayOrderBO;
import io.xiyou.order.api.dto.OrderDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.jms.Destination;
import java.util.List;

/**
 * 订单接口
 *
 * @author 西柚
 * @date 2021-10-14 00:23:08
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/order")
@Api(value = "/order", tags = "订单接口")
public class OrderController {

    @Autowired
    private IOrderService orderService;

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired
    RabbitTemplate rabbitTemplate;  //使用RabbitTemplate,这提供了接收/发送等等方法

    @Value("${spring.rabbitmq.queue-name}")
    private String queueName;

    @Value("${spring.rabbitmq.exchange-name}")
    private String exchangeName;

    @Value("${spring.rabbitmq.routing-name}")
    private String routingName;

    @ApiOperation("创建异步订单")
    @PostMapping("/createAsynOrder")
    public String createAsynOrder(@RequestBody AddOrderBO addOrderBO) {
//        this.sendMessage(this.queue, JSON.toJSONString(addOrderBO));
//        log.info("Publisher sendes Topic... ");

        //将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
        rabbitTemplate.convertAndSend(exchangeName, routingName, addOrderBO);

        return "success";
    }

    @ApiOperation("创建订单")
    @PostMapping("/createOrder")
    public OrderDTO createOrder(@RequestBody AddOrderBO addOrderBO) {
        return orderService.createOrder(addOrderBO);
    }

    @ApiOperation("批量创建订单")
    @PostMapping("/batchCreateOrder")
    public void batchCreateOrder() {
        orderService.batchCreateOrder();
    }

    @ApiOperation("支付订单")
    @PostMapping("/payOrder")
    public void payOrder(@RequestBody PayOrderBO payOrderBO) {
        payOrderBO.setXid(payOrderBO.getOrderCode() + "_pay");

        orderService.payOrder(payOrderBO);
    }

    @ApiOperation("订单详情")
    @PostMapping("/getOrderDetails")
    @ApiImplicitParam(name = "orderCode", value = "订单号", required = true, dataType = "Long", paramType = "query")
    public OrderDTO getOrderDetails(@RequestParam("orderCode") Long orderCode) {
        return orderService.getOrderDetails(orderCode);
    }

    @ApiOperation("根据用户ID查询订单列表")
    @PostMapping("/getByUserId")
    @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "Long", paramType = "query")
    public List<OrderDTO> getByUserId(@RequestParam("userId") Long userId) {
        return orderService.getByUserId(userId);
    }

    // 发送消息，destination是发送到的队列，message是待发送的消息
    private void sendMessage(Destination destination, final String message) {
        jmsMessagingTemplate.convertAndSend(destination, message);
    }

}
